/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.media.web;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.hk.media.entity.HkMediaUserShopAreaProvince;
import com.thinkgem.jeesite.modules.hk.media.entity.HkMediaUserShopRegion;
import com.thinkgem.jeesite.modules.hk.media.service.HkMediaUserShopRegionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 自媒体电商地区管理Controller
 * @author 罗天文
 * @version 2018-06-19
 */
@Controller
@RequestMapping(value = "${adminPath}/media/hkMediaUserShopRegion")
public class HkMediaUserShopRegionController extends BaseController {

	@Autowired
	private HkMediaUserShopRegionService hkMediaUserShopRegionService;
	
	@ModelAttribute
	public HkMediaUserShopRegion get(@RequestParam(required=false) String id) {
		HkMediaUserShopRegion entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = hkMediaUserShopRegionService.get(id);
		}
		if (entity == null){
			entity = new HkMediaUserShopRegion();
		}
		return entity;
	}
	
	@RequiresPermissions("media:hkMediaUserShopRegion:view")
	@RequestMapping(value = {"list", ""})
	public String list(HkMediaUserShopRegion hkMediaUserShopRegion, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<HkMediaUserShopRegion> page = hkMediaUserShopRegionService.findPage(new Page<HkMediaUserShopRegion>(request, response), hkMediaUserShopRegion); 
		model.addAttribute("page", page);
		return "hk/media/hkMediaUserShopRegionList";
	}

	@RequiresPermissions("media:hkMediaUserShopRegion:view")
	@RequestMapping(value = "form")
	public String form(HkMediaUserShopRegion hkMediaUserShopRegion, Model model) {
		model.addAttribute("hkMediaUserShopRegion", hkMediaUserShopRegion);
		return "hk/media/hkMediaUserShopRegionForm";
	}

	@RequiresPermissions("media:hkMediaUserShopRegion:edit")
	@RequestMapping(value = "save")
	public String save(HkMediaUserShopRegion hkMediaUserShopRegion, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, hkMediaUserShopRegion)){
			return form(hkMediaUserShopRegion, model);
		}
		hkMediaUserShopRegionService.save(hkMediaUserShopRegion);
		addMessage(redirectAttributes, "保存自媒体电商地区管理成功");
		return "redirect:"+Global.getAdminPath()+"/media/hkMediaUserShopRegion/?repage";
	}
	
	@RequiresPermissions("media:hkMediaUserShopRegion:edit")
	@RequestMapping(value = "delete")
	public String delete(HkMediaUserShopRegion hkMediaUserShopRegion, RedirectAttributes redirectAttributes) {
		hkMediaUserShopRegionService.delete(hkMediaUserShopRegion);
		addMessage(redirectAttributes, "删除自媒体电商地区管理成功");
		return "redirect:"+Global.getAdminPath()+"/media/hkMediaUserShopRegion/?repage";
	}


	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "chinaProviceData")
	public List<Map<String, Object>> chinaProviceData(@RequestParam(required=false) String extId, HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<HkMediaUserShopRegion> list = hkMediaUserShopRegionService.findAll();
		for (int i=0; i<list.size(); i++){
			HkMediaUserShopRegion e = list.get(i);
			Map<String, Object> map2 = Maps.newHashMap();
			map2.put("id", e.getId());
			map2.put("pId","0");
			map2.put("name",e.getName());
			mapList.add(map2);
			for (HkMediaUserShopAreaProvince a:e.getHkMediaUserShopAreaProvinceList()
				 ) {
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", a.getProvince().getId());
				map.put("pId", a.getRegion().getId());
				map.put("name",a.getProvince().getName());
				mapList.add(map);
			}


		}
		return mapList;
	}
}